import { defineStore } from "pinia";
import {tximgUrl} from "@/api/user.js"
import VueCookies from 'vue-cookies'
import {bus} from "@/utils/mitt.js";
import {ImgURL} from "@/utils/Request.js";
const useLoginStore = defineStore('loginState',{
    state:()=>{
        return{
            showLogin:false,
            username:'',
            uid:'',
            imageUrl:'',
            role:'',
            email:'',
            token:'',
        }
    },
    actions:{
        setLogin(show){
            if(show == true){
                bus.emit('login-copdown',0)
            }else{
                bus.emit('login-copdown',1)
            }
        },
        setToken(token){
            VueCookies.set('token',this.token)
            this.token = token;
        },
        async saveUserInfo(info) {
            VueCookies.set('token',this.token)
            this.imageUrl = ImgURL + "/img/tximg/" + info.imageUrl;
            this.username = info.username;
            this.uid = info.id;
            this.role = info.role;
            this.email = info.email;
        },
        dellUserInfo(){
            VueCookies.remove('token')
            this.imageUrl = "",
            this.username = "",
            this.uid = "",
            this.role = "",
            this.email = "",
            this.token = "",
            this.showLogin=false
        }
    }
},{
    persist:{
        enabled:true,//开启持久化
        key:'useLoginStore',//持久化存储的key
        storage:localStorage,
    }
})
export {
    useLoginStore
}